package kr.hkit.androidcomplete01.ch11;

import kr.hkit.androidcomplete01.R;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.widget.TextView;

public class TextViewHtmlEx extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.ch11_textviewhtmlex);
		
		TextView text = (TextView) findViewById(R.id.text);
		text.setText(Html.fromHtml(""
				+ "This <b>text</b> is <i>spanned</i> from <u>html</u> "
				+ "<font color='#ff0000'>document</font>"));
		
		TextView img = (TextView) findViewById(R.id.image);
		img.setText(Html.fromHtml("This is a androboy "
				+ "<img src=\"ic_launcher\"/> image.", new ImageGetter(), null));
	}
	
	public class ImageGetter implements Html.ImageGetter {
		@Override
		public Drawable getDrawable(String source) {
			int id = 0;
			
			if (source.equals("ic_launcher")) {
				id = R.drawable.ic_launcher;
			}
			
			if (id!=0) {
				Drawable drawable = getResources().getDrawable(id);
				drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
				return drawable;
			}
			return null;
		}
	}
}
